package com.anywhere.freightman.system.service.impl;

import com.anywhere.freightman.common.BaseEntityService;
import com.anywhere.freightman.common.BaseMapper;
import com.anywhere.freightman.common.entity.user.RoleQueryEntity;
import com.anywhere.freightman.common.ex.CustomException;
import com.anywhere.freightman.system.dao.RoleDao;
import com.anywhere.freightman.system.model.Resource;
import com.anywhere.freightman.system.model.Role;
import com.anywhere.freightman.system.model.UserModel;
import com.anywhere.freightman.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;
import java.util.Set;

@Service
@Transactional
public class RoleService extends BaseEntityService<Role, RoleQueryEntity> implements IRoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public BaseMapper<Role> getBaseDao() {
        return roleDao;
    }

    @Override
    public Example getExample(RoleQueryEntity entity) {
        Weekend<Role> weekend = Weekend.of(Role.class);
        WeekendCriteria<Role, Object> criteria = weekend.weekendCriteria();

        if (entity == null) {
            return weekend;
        }

        if (entity.getTenantId() != null) {
            criteria.andEqualTo("tenantId", entity.getTenantId());
        }

        if (!StringUtils.isEmpty(entity.getName())) {
            criteria.andLike("name", "%" + entity.getName() + "%");
        }

        return weekend;
    }

    @Override
    public List<Role> getListByUserId(long userId) {
        return roleDao.getListByUserId(userId);
    }

    @Override
    public Set<Resource> getResource(long roleId) {
        return roleDao.getResource(roleId);
    }

    @Override
    public Set<Resource> getResourceByUserId(long userId) {
        return roleDao.getResourceByUserId(userId);
    }

    @Override
    public List<Role> getListByTenantId(long tenantId) {
        return roleDao.getListByTenantId(tenantId);
    }

    @Override
    public void deleteRole(Long userId) {
        roleDao.deleteRole(userId);
    }

    @Override
    public void insertRole(Long userId, List<Long> roleIds) {
        roleDao.insertRole(userId, roleIds);
    }

    @Override
    public void insert(Role model, List<Long> resIds) {
        super.insert(model);
        roleDao.insertRoleRes(model.getId(), resIds);
    }

    @Override
    public void update(Role model, List<Long> resIds) {
        super.update(model);
        roleDao.deleteRoleRes(model.getId());
        roleDao.insertRoleRes(model.getId(), resIds);
    }

    @Override
    public List<UserModel> getUserListByRoleId(long roleId) {
        return roleDao.getUserListByRoleId(roleId);
    }

    @Override
    public Role getByCode(String code) {
        return roleDao.getByCode(code);
    }

    @Override
    public void delete(Role model) {
        List<UserModel> userList = getUserListByRoleId(model.getId());
        if (userList.size() > 0) {
            String[] usernameList = {""};
            userList.forEach(item -> {
                usernameList[0] += item.getUsername() + "; ";
            });
            throw new CustomException(String.format("该角色分配给了一下用户，不能删除! [%s]", usernameList[0]));
        }

        super.delete(model);
    }
}
